package com.wjc.wjc.web.admin;


import com.wjc.wjc.service.RecordService;
import com.wjc.wjc.unity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class RecordController {


    @Autowired
    private RecordService recordService;

    @GetMapping("/admin/record")
    public String record(Model model){

        model.addAttribute("re",recordService.lsitRecord());
        return "admin/record";
    }


    @GetMapping("/record/input")
    public String input(){
        return "admin/record-input";
    }

    @PostMapping("/record/save")
    public String save(Record record, RedirectAttributes attributes){
        Record r=recordService.save(record);
        if (r!=null){
            return "redirect:/admin/record";
        }
        attributes.addFlashAttribute("message","保存失败");
        return "redirect:/record/input";
    }

    @GetMapping("/record/{id}/update")
    public String updateOp(@PathVariable Long id){
        recordService.updataOp(id);
        return "redirect:/admin/record";
    }

    @GetMapping("/record/{id}/delete")
    public String delete(@PathVariable Long id){
        recordService.delete(id);
        return "redirect:/admin/record";
    }

    @PostMapping("/notice/search")
    public String search(@RequestParam boolean opinion,Model model){
        model.addAttribute("re",recordService.lsitRecord(opinion));
        return "/admin/record :: rs";
    }

}
